package com.hengx.cellview.cell;

import static com.hengx.cellview.util.GravityUtils.*;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.view.Gravity;

import java.util.ArrayList;
import java.util.List;

public class TextCell extends Cell {

    private String text;
    private float textSize;
    private int textColor;
    private int backgroundColor;
    private int gravity;
    private float verticalSpacing;
    private TextPaint paint;
    private List<String> lines;

    public TextCell(String text) {
        this(text, 0xff000000);
    }

    public TextCell(String text, int textColor) {
        this(text, textColor, 0x00);
    }

    public TextCell(String text, int textColor, int backgroundColor) {
        this(text, textColor, backgroundColor, 30f);
    }

    public TextCell(String text, int textColor, int backgroundColor, float textSize) {
        this.text = text;
        this.textColor = textColor;
        this.backgroundColor = backgroundColor;
        this.textSize = textSize;
        this.verticalSpacing = 3f;
        this.gravity = Gravity.CENTER;
        this.lines = new ArrayList<>();
    }

    @Override
    public synchronized void draw(Paint paint, Canvas canvas) {
        RectF bounds = getBounds();
        if (Color.alpha(backgroundColor) > 0) {
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(backgroundColor);
            canvas.drawRect(bounds, paint);
        }
        float baseX = bounds.left;
        float baseY = bounds.top;
        lines.clear();
        if (Color.alpha(textColor) > 0) {
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(textSize);
            paint.setColor(textColor);
            canvas.save();
            canvas.clipRect(bounds);
            String[] lineP = text.split("\n");
            int li = 0;
            for(String lp : lineP) {
                String part = lp;
                if (li < lineP.length - 1) part += '\n';
                while(!part.isEmpty()) {
                    int index = paint.breakText(part, false, bounds.width(), null);
                    String get = part.substring(0, index);
                    lines.add(get);
                    part = part.substring(index);
                }
                li++;
            }
            float lineHeight = textSize + (verticalSpacing * 2f);
            float maxHeight = lineHeight * lines.size();
            if (containsBottom(gravity)) {
                baseY = Math.max(bounds.top, bounds.bottom - maxHeight);
            } else if (containsCenterVertical(gravity)) {
                baseY = Math.max(bounds.top, bounds.top + (bounds.height() / 2f) - (maxHeight / 2f));
            }
            float ascent = Math.abs(paint.ascent());
            for(int i = 0; i < lines.size(); i++) {
                float lineY = baseY + (i * lineHeight);
                if (lineY > bounds.bottom) break;
                String line = lines.get(i).replace("\n", "");
                float x;
                if (containsLeft(gravity)) {
                    x = baseX;
                } else if (containsRight(gravity)) {
                    x = Math.max(baseX, baseX + bounds.width() - paint.measureText(line));
                } else {
                    x = Math.max(baseX, baseX + (bounds.width() / 2f) - (paint.measureText(line) / 2f));
                }
                float y = lineY + ((lineHeight / 2f) - (paint.descent() + paint.ascent()) / 2f);
                if (this.paint != null) {
                    canvas.drawText(line, x, y, this.paint);
                }else {
                    canvas.drawText(line, x, y, paint);
                }
                
                /*
                * 调试代码，绘制文本基线
                paint.setColor(0xffff0000);
                canvas.drawLine(baseX, baseY + Math.abs(paint.ascent()), bounds.right, baseY + Math.abs(paint.ascent()), paint);
                paint.setColor(0xff0000ff);
                canvas.drawLine(baseX, baseY + paint.descent(), bounds.right, baseY + paint.descent(), paint);
                */
            }
            canvas.restore();
        }
    }

    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public float getTextSize() {
        return this.textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public int getTextColor() {
        return this.textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public int getBackgroundColor() {
        return this.backgroundColor;
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public int getGravity() {
        return this.gravity;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    public TextPaint getPaint() {
        return this.paint;
    }

    public void setPaint(TextPaint paint) {
        this.paint = paint;
    }

    public float getVerticalSpacing() {
        return this.verticalSpacing;
    }
    
    public void setVerticalSpacing(float verticalSpacing) {
        this.verticalSpacing = verticalSpacing;
    }
    
    public String[] getLines() {
    	return this.lines.toArray(new String[0]);
    }
    
}